//返回在某个字符存在多少次 下标分别在哪儿
function searchSubStr(str,subStr) {
    var positions = new Array();
    var pos = str.indexOf(subStr);
    while (pos > -1) {
        positions.push(pos);
        pos = str.indexOf(subStr, pos + 1);
    }
    return positions;
}

//获取当天日期    不带时分秒
function getTodayOnly() {
    var date = new Date();
    var date = new Date();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if(month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if(strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + "-" + month + "-" + strDate;
    return currentdate;
}
//对题目的图片进行处理进行显示
function itemContentDisposeShow (itemContent) {

    var count = 0;
    var indexOfRigtn="";
    var positionsImageList =   searchSubStr(itemContent, "#");
    var imgArray = [];
    for (var i = 0; i < positionsImageList.length; i = i + 2) {
        var imgStr = itemContent.substring(positionsImageList[i], positionsImageList[i + 1] + 1);
        imgArray.push(imgStr);
        count++;
        if (count > 100) break; //防止死循环
    }
    for (var i = 0 ; i< imgArray.length;i++){
        var  html = "";
        html = '<img src="/tu/upload/imageUrl?fileName='+imgArray[i].split("#")[1].split("image=")[1]+'" title="'+imgArray[i].split("#")[1].split("image=")[1]+'" alt="'+imgArray[i].split("#")[1].split("image=")[1]+'"/>';
        itemContent = itemContent.replace(imgArray[i],html)
    }
    return itemContent;
}

//拿到所有图片位置
function searchSubStr(str,subStr) {
    var positions = new Array();
    var pos = str.indexOf(subStr);
    while(pos>-1){
        positions.push(pos);
        pos = str.indexOf(subStr,pos+1);
    }
    return positions;
}



//对题目的图片进行解析入库
function itemContentDisposeInDataBase(itemContent) {
    var indexOfLeft = searchSubStr(itemContent,"<img");
    for(var i in indexOfLeft){
        var indexOfRigtn =  itemContent.indexOf("/>");
        //范围截取的图片范围
        var imageScope =   itemContent.substring(itemContent.indexOf("<img"),indexOfRigtn+2);
        //图片
        var  imageData =  imageScope.split('alt="')[1].split('"/>')[0];
        itemContent = itemContent.replace(imageScope,"#image="+imageData+"#");
    }
    itemContent = itemContent.replace("<p>","")
    itemContent = itemContent.replace("</p>","");
    return itemContent;

    
}



//对【】进行解析
function  removeSquareBrackets(contentData) {
    //出现次数
    var occurrenceNumber =   searchSubStr(contentData, "【");
    for (var i = 0 ; i< occurrenceNumber.length;i++){
        var head = contentData.indexOf("【");
        var behind = contentData.indexOf("】");
        var contentIndexData =  contentData.substring(head, behind+1);
        //以（答案的形式拼接）
        contentData = contentData.replace(contentIndexData,"<span style='color: #BBBBBB'>("+contentIndexData.split("【")[1].split("】")[0]+")</span>");
    }
    return contentData;
}
//解码base64
this.decode = function (input) {
    var  _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
        enc1 = _keyStr.indexOf(input.charAt(i++));
        enc2 = _keyStr.indexOf(input.charAt(i++));
        enc3 = _keyStr.indexOf(input.charAt(i++));
        enc4 = _keyStr.indexOf(input.charAt(i++));
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output = output + String.fromCharCode(chr1);
        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }
    }
    output = _utf8_decode(output);
    return output;
}

var _utf8_decode = function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
    while ( i < utftext.length ) {
        c = utftext.charCodeAt(i);
        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        } else if((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    return string;
}

